/*
 * @Author: Marte
 * @Date:   2016-10-25 18:06:51
 * @Last Modified by:   Marte
 * @Last Modified time: 2016-11-11 11:37:35
 */

'use strict';

var currApp = angular.module('piclist.app', ['ui.bootstrap', 'qf.common']);

currApp.controller('pagectrl', ['$scope', 'dialogService', '$http', '$window', '$modal',
    function($scope, dialogService, $http, $window, $modal) {
        $scope.globalParam = globalParam;

        var vm = this;
        var typeId = getParamByName('typeid');
        var remarkId = getParamByName('remarkid');

        vm.type = getParamByName('type');
        vm.remark = getParamByName('remark');

        $scope.editParamString = $scope.globalParam.paramString() + '&typeid=' + typeId + '&remarkid=' + remarkId + '&type=' + vm.type + '&remark=' + vm.remark;

        //获取相片
        $http.get('api/picTypeRemarkGroups?typeid=' + typeId + '&remarkid=' + remarkId)
            .success(function (data, status, headers, config) {
                //console.log(data);
                var result = data;
                if (!result.error) {
                    var obj = {};
                    //按评语分组分组显示
                    for (var i = 0; i < result.data.length; i++) {
                        if (!obj[result.data[i].REMARKGROUP.GROUPNAME]) {
                            obj[result.data[i].REMARKGROUP.GROUPNAME] = [
                                result.data[i].PIC
                            ];
                        } else {
                            obj[result.data[i].REMARKGROUP.GROUPNAME].push(result.data[i].PIC);
                        }
                    }
                    //console.log(obj);
                    vm.picArr = result.data.length > 0 ? obj : null;
                } else {
                    vm.picArr = {};
                }
            })
            .error(function(data, status, headers, config) {
                vm.picArr = {};
            });

        //删除当前证件类型、评语的相片以及相片分组
        vm.delAll = function() {
            dialogService.confirm({
                    msg: '你确定要删除“' + vm.type + '”——“' + vm.remark + '”的相片吗？'
                },
                function() {
                    $http({ method: 'DELETE', url: 'api/pictyperemarkgroups?typeid=' + typeId + '&remarkid=' + remarkId + '&userid=' + $scope.globalParam.uid })
                        .success(function(data, status, headers, config) {
                            if (data.error) {
                                dialogService.showmessage({ msg: data.error });
                            } else {
                                $window.location.replace('index.html?' + $scope.globalParam.paramString());
                            }
                        })
                        .error(function(data, status, headers, config) {
                            dialogService.showmessage({ msg: '删除失败' });
                        });
                },
                function() {

                });
        };

        $scope.showImage = function(groupName, index) { //点击图片时，打开模态框
            dialogService.showImageBox({
                images: vm.picArr[groupName],
                currIndex: index
            });

        };
    }
]);
